package com.cm.projectx.util;

import cn.hutool.core.util.StrUtil;
import com.cm.projectx.entity.analysis.RespDto;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class PoiHelper {
    /**
     * 文件下载
     *
     * @param fileInfo
     * @param response
     */
    public static void downFileByBrowser(Map<String, Object> fileInfo, HttpServletResponse response) {
        BufferedOutputStream outputStream = null;
        POIXMLDocument document = null;
        try {
            //获取输出流
            outputStream = new BufferedOutputStream(response.getOutputStream());

            //抽取文件信息
            String fileName = (String) fileInfo.get("excel_file_name");
            document = (POIXMLDocument) fileInfo.get("excel_file_content");

            //执行excel公式
            if (document instanceof XSSFWorkbook) {
                XSSFWorkbook workbook = (XSSFWorkbook) document;
                for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                    Sheet sheet = workbook.getSheetAt(i);
                    sheet.setForceFormulaRecalculation(true);
                }
            }

            //设置文件下载响应头信息
            response.reset();
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setContentType("application/octet-stream");

            //文件下载
            document.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != document) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 返回与给定行样式相同的空行
     *
     * @param sheet
     * @param baseRowNum
     * @return
     */
    public static XSSFRow getNextXssfRow(XSSFSheet sheet, int baseRowNum) {
        int lastRowNum = sheet.getLastRowNum();
        XSSFRow lastRow = sheet.getRow(lastRowNum);
        XSSFRow nextRow = sheet.getRow(lastRowNum + 1);
        if (isEmptyRow(lastRow) && nextRow == null) {
            return lastRow;
        } else {
            nextRow = sheet.createRow(lastRowNum + 1);
            copyRowXssf(sheet , nextRow, sheet.getRow(baseRowNum));
            return nextRow;
        }
    }

    /**
     * 判断某行是否有内容
     *
     * @param row
     * @return
     */
    public static boolean isEmptyRow(Row row) {
        return row != null && (isEmptyCell(row.getCell(0)) && isEmptyCell(row.getCell(1))
                && isEmptyCell(row.getCell(2)) && isEmptyCell(row.getCell(3))
                && isEmptyCell(row.getCell(4)) && isEmptyCell(row.getCell(5))
                && isEmptyCell(row.getCell(6)));
    }
    /**
     * 判断单元格是否有内容
     *
     * @param cell
     * @return
     */
    public static boolean isEmptyCell(Cell cell) {
        if (cell == null) {
            return true;
        }

        String cellValue;
        if (cell.getCellType() == CellType.NUMERIC) {
            cellValue = cell.getNumericCellValue() + "";
        } else if (cell.getCellType() == CellType.FORMULA) {
            cellValue = "";
        } else {
            cellValue = cell.getStringCellValue();
        }

        return StrUtil.isEmpty(cellValue);
    }

    /**
     * 依据给定行同化两行的样式
     *
     * @param row
     * @param sourceRow
     */
    public static void copyRowXssf(XSSFSheet sheet, Row row, Row sourceRow) {
        int num = 0;
        while (sourceRow != null && sourceRow.getCell(num) != null) {
            Cell cell = row.createCell(num);
            cell.setCellStyle(sourceRow.getCell(num).getCellStyle());
            //复制公式
            if (sourceRow.getCell(num).getCellType() == CellType.FORMULA) {
                String formula = sourceRow.getCell(num).getCellFormula();
                formula = formula.replace(String.valueOf(sourceRow.getRowNum() + 1), String.valueOf(row.getRowNum() + 1));
                cell.setCellFormula(formula);
            }
            num++;
        }

        if (row != null && sourceRow != null) {
            row.setHeight(sourceRow.getHeight());
        }
    }
    /**
     * 表格cell赋值
     * @param cell
     * @param value
     */
    public static void setValueToCellV2(XSSFCell cell, Object value) {
        cell.setCellValue(StrUtil.toStringOrNull(value));
    }

}
